<template>
  <div class="monitor">
    <div class="title">
      <p class="tip">灾备问题</p>
      <button @click="back" class="goBack">返回</button>
    </div>
    <div class="contain">
      <el-container>
        <el-header>
          <el-menu
            :default-active="activeIndex"
            class="el-menu-demo"
            mode="horizontal"
            @select="handleSelect"
          >
            <el-menu-item index="1">问题单涉及系统总数</el-menu-item>
            <el-menu-item index="2">已关闭问题单涉及系统</el-menu-item>
          </el-menu>
        </el-header>
        <el-main>
          <div class="main">
            <el-form ref="searchForm" :inline="true" class="demo-form-inline first">
              <el-form-item label="系统编号">
                <el-input
                  clearable
                  v-model="changeCode"
                  class="inline-input"
                  placeholder="请输入系统编号"
                />
              </el-form-item>
              <el-form-item label="系统名称">
                <el-input
                  clearable
                  v-model="sysName"
                  class="inline-input"
                  placeholder="请输入系统名称"
                />
              </el-form-item>
              <el-form-item label="是否重要系统">
                <el-select v-model="importSys" clearable>
                  <el-option label="是" value="1" />
                  <el-option label="否" value="0" />
                </el-select>
              </el-form-item>
              <!-- <el-form-item label="查询范围" prop="">
                <el-date-picker v-model="value2" type="month" placeholder="Pick a month" />
              </el-form-item> -->
            </el-form>

            <div class="button">
              <!-- <button @click="detail" class="btn">查看详情</button> -->
              <button @click="search" @keyup.enter="monitor()" class="btn reset"> 搜索 </button>
              <button @click="resetForm" class="btn">重置</button>
              <el-dropdown trigger="click">
                <span class="el-dropdown-link">
                  导出<i class="el-icon-arrow-down el-icon--right"></i>
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item icon="el-icon-plus" @click="downloadExcel">
                      导出当前页</el-dropdown-item
                    >
                    <el-dropdown-item icon="el-icon-circle-plus" @click="Select_downloadExcel">
                      导出所有页</el-dropdown-item
                    >
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </div>
          </div>
          <div class="tableBox">
            <div class="desc">
              <img src="@/assets/images/ywctb.png" alt="" />
              {{ desc }}
            </div>
            <div class="table" ref="container">
              <el-table
                :height="tableHeight"
                ref="multipleTableRef"
                :data="changeList"
                tooltip-effect="dark"
                @select="selectChange"
                @selection-change="handleSelectionChange"
                row-key="getRowKey"
                @row-click="handleRowClick"
                v-loading="loading"
                element-loading-text="加载中"
              >
                <el-table-column
                  align="center"
                  :reserve-selection="false"
                  type="selection"
                ></el-table-column>
                <el-table-column
                  align="center"
                  prop="systemNo"
                  label="系统编号"
                  show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                  align="center"
                  prop="systemName"
                  label="系统名称"
                  show-overflow-tooltip
                ></el-table-column>
                <el-table-column align="center" label="是否重要系统" show-overflow-tooltip>
                  <template #default="scope">
                    <span>
                      {{ scope.row.isKeySys == 1 ? '是' : scope.row.isKeySys == '0' ? '否' : '' }}
                    </span>
                  </template>
                </el-table-column>
                <el-table-column
                  align="center"
                  prop="orgName"
                  label="所属机构"
                  show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                  align="center"
                  prop="appSubDept"
                  label="管理处室"
                  show-overflow-tooltip
                ></el-table-column>
                <el-table-column align="center" label="涉及问题单" show-overflow-tooltip>
                  <template #default="scope">
                    <el-button
                      size="small"
                      type="danger"
                      @click="handleQuestion(scope.row.systemNo)"
                    >
                      {{ scope.row.issueNumber }}
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
            </div>

            <!-- 分页 -->
            <el-pagination
              ref="pagination"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="pageNum"
              :page-sizes="[10, 15, 20]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
            ></el-pagination>
          </div>
        </el-main>
      </el-container>
    </div>
  </div>
</template>
<script setup lang="ts">
  import { ElMessage } from 'element-plus';
  import { disasterIssueBySytemNumberList, exportDisasterIssueSystemList } from '@/http/Disaster';
  import { download } from '@/http/api1';
  import { ref, onMounted, onBeforeUnmount, reactive } from 'vue';
  import { useRouter, useRoute } from 'vue-router';
  //   import { useStore } from 'vuex';
  //   const store = useStore();
  const router = useRouter();
  const route = useRoute();
  const pageNum = ref(1);
  const pageSize = ref(10);
  const total = ref(0);
  const changeList = reactive([]);
  const container = ref(null);
  const tableHeight = ref(0);
  const sysName = ref('');
  const changeCode = ref('');
  const desc = ref('');
  const activeIndex = ref(route.params.type ? route.params.type : '1');
  const multipleTableRef = ref<InstanceType<typeof ElTable>>();
  const multipleTable = ref<User[]>([]);
  const qhyljhDate = ref(route.params.qhyljhDate); // 日期
  const importSys = ref(route.params.importSys ? route.params.importSys : ''); // 是否重要系统
  const loading = ref(true);
  onMounted(() => {
    enterKeyup();
    calHeight();
    switch (activeIndex.value) {
      case '1':
        desc.value =
          '问题单涉及系统总数：查询时间范围内创建的问题类型为“灾备系统” 且 状态！=“待提交、作废”的运维问题单涉及的系统个数；';
        break;
      case '2':
        desc.value =
          '已关闭的问题单涉及系统数：查询时间范围内创建的问题类型为“灾备系统” 且 状态==“已关闭”的运维问题单设计的系统个数；';
        break;
      default:
        break;
    }
    IssueList();
  });
  const handleSelect = (key: any) => {
    activeIndex.value = key;
    loading.value = true;
    pageNum.value = 1;
    switch (activeIndex.value) {
      case '1':
        desc.value =
          '问题单涉及系统总数：查询时间范围内创建的问题类型为“灾备系统” 且 状态！=“待提交、作废”的运维问题单涉及的系统个数；';
        break;
      case '2':
        desc.value =
          '已关闭的问题单涉及系统数：查询时间范围内创建的问题类型为“灾备系统” 且 状态==“已关闭”的运维问题单设计的系统个数；';
        break;
      default:
        break;
    }
    IssueList();
  };
  const IssueList = () => {
    disasterIssueBySytemNumberList({
      qhyljhDate: qhyljhDate.value,
      issueStatus: activeIndex.value,
      pageNum: pageNum.value,
      pageSize: pageSize.value,
      systemNo: changeCode.value,
      isKeySys: importSys.value,
      systemName: sysName.value
    }).then((res) => {
      if ((res as any).data.code !== 0) {
        ElMessage({
          message: (res as any).data.msg,
          type: 'error'
        });
        return;
      }
      loading.value = false;
      changeList.length = 0;
      (changeList as any).push(...res.data.data.rows);
      total.value = res.data.data.total;
    });
  };
  const handleQuestion = (row: User) => {
    router.push({
      name: 'Question_sheet',
      params: {
        type: activeIndex.value,
        systemNo: row,
        importSys: importSys.value,
        qhyljhDate: qhyljhDate.value
      }
    });
  };
  const monitor = () => {
    document.onkeydown = (e) => {
      if (e.keyCode == 13) {
        search();
      }
    };
  };
  const enterKeyupDestroyed = () => {
    document.removeEventListener('keyup', monitor());
  };
  const enterKeyup = () => {
    document.addEventListener('keyup', monitor());
  };

  const calHeight = () => {
    const rect = container.value.getBoundingClientRect();
    tableHeight.value = rect.height;
  };
  // 导出当前页
  const downloadExcel = () => {
    // Export2Excel(this.columns, this.changeList);
    if (changeList.length === 0) {
      ElMessage({
        message: '当前暂无数据',
        type: 'warning'
      });
      ElMessage({
        message: '当前暂无数据',
        type: 'error'
      });
      return;
    }
    exportlist(changeList.value);
  };
  const exportlist = () => {
    exportDisasterIssueSystemList({
      qhyljhDate: qhyljhDate.value,
      issueStatus: activeIndex.value,
      pageNum: JSON.stringify(pageNum.value),
      pageSize: JSON.stringify(pageSize.value),
      systemNo: changeCode.value,
      isKeySys: importSys.value,
      systemName: sysName.value,
      currentPage: 'currentPage'
    }).then((res) => {
      if ((res as any).data.code !== 0) {
        ElMessage({
          message: (res as any).data.msg,
          type: 'error'
        });
        return;
      }
      download_list(res.data.msg);
    });
  };
  const download_list = (type) => {
    download({
      fileName: type,
      delete: true
    }).then((res) => {
      const link = document.createElement('a');
      let blob = new Blob([res.data], {
        type: 'application/vnd.ms-excel'
      });
      link.style.display = 'none';
      link.href = URL.createObjectURL(blob);
      link.download = type; //下载的文件名
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    });
  };
  // 导出所有的
  const Select_downloadExcel = () => {
    exportDisasterIssueSystemList({
      qhyljhDate: qhyljhDate.value,
      issueStatus: activeIndex.value,
      pageNum: JSON.stringify(pageNum.value),
      pageSize: JSON.stringify(pageSize.value),
      systemNo: changeCode.value,
      isKeySys: importSys.value,
      systemName: sysName.value,
      currentPage: ''
    }).then((res) => {
      if ((res as any).data.code !== 0) {
        ElMessage({
          message: (res as any).data.msg,
          type: 'error'
        });
        return;
      }
      download_list(res.data.msg);
    });
  };

  // 重置
  const resetForm = () => {
    changeCode.value = '';
    sysName.value = '';
    importSys.value = '';
  };
  const search = () => {
    changeCode.value = changeCode.value.replace(/(^\s*)|(\s*$)/g, '');
    sysName.value = sysName.value.replace(/(^\s*)|(\s*$)/g, '');
    pageNum.value = 1;
    IssueList();
  };
  // 分页
  const handleSizeChange = (size) => {
    pageSize.value = size;
    loading.value = true;
    IssueList();
  };
  const handleCurrentChange = (num) => {
    pageNum.value = num;
    loading.value = true;
    IssueList();
  };

  const back = () => {
    // this.$router.go(-1);
    router.push({
      name: 'Disaster'
    });
  };

  // 点击行触发，选中或不选中复选框
  const handleRowClick = (row) => {
    multipleTable.value = row;
    multipleTableRef.value.toggleRowSelection(row);
    selectChange(multipleTable.value);
  };
  const selectChange = (selection: string) => {
    if (selection.length > 1) {
      const del_row = selection.shift();
      multipleTableRef.value!.toggleRowSelection(del_row);
    }
  };
  const handleSelectionChange = (val: string) => {
    multipleTable.value = val;
  };

  /*  const detail = () => {
    if (!multipleTable.value.length) {
      ElMessage({
        message: '请先勾选查看的数据',
        type: 'warning'
      });
    } else {
    }
  }; */
  onBeforeUnmount(() => {
    enterKeyupDestroyed();
  });
</script>
<style lang="less" scoped>
  .monitor {
    width: 100%;
    height: 100vh;
    background: #eaeef4;
    padding: 20px;
    box-sizing: border-box;
    //background-image: url("../../assets/images/background-image.jpg");

    .title {
      display: flex;
      justify-content: space-between;
      margin-bottom: 15px;
      .tip {
        height: 34px;
        line-height: 34px;
        font-size: 18px;
        font-weight: 400;
        color: #46545d;
        margin: 0;
        padding: 0;
      }
      .goBack {
        background: #007af5;
        width: 80px;
        height: 34px;
        border: none;
        color: #fff;
        font-size: 12px;
        font-weight: 400;
        cursor: pointer;
      }
    }
    .tabs_item {
      height: calc(100% - 50px);
      background: #fff;
      padding: 20px;
      box-sizing: border-box;
      :deep(.el-tabs) {
        height: 100%;
        .el-tabs__header {
          .el-tabs__nav-wrap {
            .el-tabs__nav-scroll {
              .el-tabs__nav {
                .el-tabs__active-bar {
                  height: 2px;
                }
                .el-tabs__item {
                  font-size: 16px;
                  font-weight: 400;
                  color: #46545d;
                  &.is-active {
                    font-weight: bold;
                    color: #007af5;
                  }
                }
              }
            }
          }
        }
        .el-tabs__content {
          height: calc(100% - 40px);
          .el-tab-pane {
            height: 100%;
          }
        }
      }
    }
    .contain {
      background: #fff;
      height: calc(100% - 50px);
      .el-container {
        height: 100%;
        .el-header {
          .el-menu--horizontal {
            border-bottom: none;
            .el-menu-item {
              font-size: 16px;
              font-weight: 400;
              color: #46545d;
              &.is-active {
                font-size: 16px;
                font-weight: bold;
                color: #007af5;
                border-bottom: 2px solid #007af5;
              }
            }
          }
        }
        .el-main {
          .main {
            // display: flex;
            // justify-content: space-between;
            margin-bottom: 15px;
            .demo-form-inline {
              display: flex;
              height: 34px;
              // max-width: 70%;
              // width: 70%;
              //align-items: center;
              :deep(input::placeholder) {
                font-size: 14px;
                font-weight: 400;
                color: #bbbbbb;
              }
              :deep(.el-form-item) {
                margin-bottom: 0;
                line-height: 34px;
                height: 34px;
                width: 40%;
              }
              :deep(.el-form-item__content) {
                line-height: 34px;
                flex-wrap: nowrap;
                //height: 43px;
                // width: 80%;
                :deep(.inline-input) {
                  // width: 230px;
                  // height: 34px;
                  .el-input__wrapper {
                    :deep(.el-input__inner) {
                      // width: 200px;
                      // height: 34px;
                      background: #ffffff;
                      // border: 1px solid #dddddd;
                      border-radius: 2px;
                    }
                  }
                }
                .btn {
                  width: 80px;
                  height: 34px;
                  line-height: 34px;
                  background: #007af5;
                  border-radius: 2px;
                  font-size: 12px;
                  font-weight: 400;
                  color: #ffffff;
                  border: 0;
                  cursor: pointer;
                  &.reset {
                    margin: 0 10px;
                  }
                  &.deatil {
                    min-width: 80px;
                  }
                }
                #goBack {
                  max-width: 131px;
                  height: 34px;
                  background: #007af5;
                  border-radius: 2px;
                  border: 0;
                  font-size: 12px;
                  font-weight: 400;
                  color: #ffffff;
                  cursor: pointer;
                  box-sizing: border-box;
                }
                .el-dropdown {
                  margin: 0px 10px;
                  cursor: pointer;
                }
                .el-dropdown-link {
                  // display: inline-block;
                  width: 80px;
                  height: 34px;
                  line-height: 34px;
                  text-align: center;
                  color: #fff;
                  background: #007af5;
                  border-radius: 2px;
                }
              }

              :deep(.el-form-item__label) {
                // color: #6fd7f7;
                font-size: 14px;
                font-weight: 400;
                color: #46545d;
                height: 34px;
                line-height: 34px;
              }
              &.first {
                margin-bottom: 10px;
              }
            }
            .button {
              // max-width: 40%;
              display: flex;
              justify-content: flex-end;
              .btn {
                width: 80px;
                height: 34px;
                line-height: 34px;
                background: #007af5;
                border-radius: 2px;
                font-size: 12px;
                font-weight: 400;
                color: #ffffff;
                border: 0;
                padding: 0;
                cursor: pointer;
                &.reset {
                  margin: 0 10px;
                }
              }
              #goBack {
                max-width: 131px;
                height: 34px;
                background: #007af5;
                border-radius: 2px;
                border: 0;
                font-size: 12px;
                font-weight: 400;
                color: #ffffff;
                cursor: pointer;
              }
              .el-dropdown {
                margin: 0px 10px;
                cursor: pointer;
              }
              .el-dropdown-link {
                // display: inline-block;
                width: 80px;
                height: 34px;
                line-height: 34px;
                text-align: center;
                color: #fff;
                background: #007af5;
                border-radius: 2px;
              }
            }
          }

          .tableBox {
            height: calc(100% - 100px);
            background: #fff;
            // padding: 20px;
            box-sizing: border-box;
            //overflow: hidden;
            .desc {
              font-size: 12px;
              font-weight: 400;
              color: #0aaf7f;
              background: rgba(9, 175, 127, 0.1);
              line-height: 33px;
              height: 33px;
              text-align: center;
              border-radius: 2px;
              display: flex;
              align-items: center;
              justify-content: center;

              img {
                width: 16px;
                height: 16px;
                // margin: auto;
                margin-right: 10px;
              }
            }
            .table {
              // background: rgba(0, 0, 0, 0.1);
              height: calc(100% - 93px);
              width: 100%;
              margin-bottom: 15px;
              :deep(.el-table-column--selection.is-leaf .el-checkbox) {
                display: none;
              }
              :deep(.el-table__cell.is-leaf) {
                //background: rgba(0, 0, 0, 0.1);
                // color: #6fd7f7;
                //color: #fff;
                color: #929ea8;
              }

              :deep(tr:hover > td) {
                background-color: transparent;
              }
              :deep(.el-table__empty-text) {
                //color: #fff;
                color: #929ea8;
              }
              :deep(td.el-table__cell) {
                //color: #fff;
                color: #929ea8;
              }
            }
            .el-pagination {
              height: 34px;
              padding: 0;
              box-sizing: border-box;
              //width: 50%;
              :deep(.el-pagination__total) {
                font-size: 14px;
                font-weight: 400;
                color: #bbbbbb;
                line-height: 34px;
              }

              :deep(.el-input__inner) {
                height: 34px;
                //width: 60px;
                background: #fff;
                //color: #66b1ff;
                color: #46545d;
              }
              :deep(button:disabled) {
                background: #fff;
                height: 34px;
                line-height: 34px;
                border: 1px solid #dddddd;
                //color: #66b1ff;
                color: #46545d;
                box-sizing: border-box;
              }
              :deep(.el-pager li) {
                //background: rgba(0, 0, 0, 0.1);
                background: #fff;
                height: 34px;
                line-height: 34px;
                border: 1px solid #dddddd;
                //color: #66b1ff;
                color: #46545d;
                box-sizing: border-box;
                &.active {
                  // color: #fff;
                  color: #46545d;
                }
              }
              :deep(.btn-prev) {
                background: #fff;
                height: 34px;
                line-height: 34px;
                border: 1px solid #dddddd;
                //color: #66b1ff;
                color: #46545d;
                box-sizing: border-box;
              }
              :deep(.btn-next) {
                background: #fff;
                height: 34px;
                line-height: 34px;
                border: 1px solid #dddddd;
                //color: #66b1ff;
                color: #46545d;
                box-sizing: border-box;
              }
              :deep(span:not([class*='suffix'])) {
                color: #bbbbbb;
              }
              .el-pagination__jump {
                height: 34px;
                line-height: 34px;
              }
            }
          }
        }
      }
    }
  }
</style>
